package com.itzdm.friend.controller;

import com.itzdm.client.client.WsClient;
import com.itzdm.common.context.UserContext;
import com.itzdm.common.result.PageResult;
import com.itzdm.common.result.Result;
import com.itzdm.friend.domain.dto.FriendRemarkDto;
import com.itzdm.friend.domain.dto.FriendRequestDto;
import com.itzdm.friend.domain.dto.HandleRequestDto;
import com.itzdm.friend.domain.po.Friend;
import com.itzdm.friend.domain.vo.FriendGetVo;
import com.itzdm.friend.domain.vo.FriendRequestVo;
import com.itzdm.friend.domain.vo.FriendSearchVo;
import com.itzdm.friend.service.IFriendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者：张德明
 * @时间：2025/4/5 15:37
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "好友管理接口")
public class FriendController {

    private final IFriendService friendService;

    private final WsClient wsClient;

    /**
     * 搜索好友
     *
     * @param keyword
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("搜索用户")
    public Result<FriendSearchVo> searchFriend(@RequestParam String keyword) {
        log.info("搜索好友: {}", keyword);
        return Result.success(friendService.searchFriend(keyword));
    }

    /**
     * 添加好友
     *
     * @param friendRequestDto
     * @return
     */
    @PostMapping("/request")
    @ApiOperation("发送好友请求")
    public Result<Void> requestFriend(@RequestBody FriendRequestDto friendRequestDto) {
        log.info("发送好友请求: {}", friendRequestDto);
        friendService.requestFriend(friendRequestDto);
        wsClient.sendMessage(friendRequestDto.getTarget(), "NEW_FRIEND");
        return Result.success();
    }

    /**
     * 查询好友请求
     *
     * @return
     */
    @GetMapping("/requests")
    @ApiOperation("获取好友请求列表")
    public Result<List<FriendRequestVo>> getFriendRequests() {
        log.info("获取好友请求列表");
        return Result.success(friendService.getFriendRequests());
    }

    /**
     * /**
     * 处理好友请求
     *
     * @param requestId
     * @param handleRequestDto
     * @return
     */
    @PostMapping("/request/{requestId}/handle")
    @ApiOperation("处理好友请求")
    public Result<Void> handleRequest(@PathVariable Long requestId, @RequestBody HandleRequestDto handleRequestDto) {
        log.info("处理好友请求: requestId={}, handleRequestDto={}", requestId, handleRequestDto);
        Friend friend = friendService.handleRequest(requestId, handleRequestDto);
        // 如果添加好友成功，推送消息
        if (handleRequestDto.getAccept()) {
            Long userId = friend.getUserId();
            wsClient.sendMessage(userId, "ACCEPT_FRIEND");
        }
        return Result.success();
    }

    /**
     * 获取我的好友列表
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页获取好友列表")
    public Result<PageResult> getMyFriendList(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false) Integer size) {
        log.info("获取我的好友列表，page={}, size={}", page, size);
        return Result.success(friendService.getMyFriendList(page, size));
    }

    /**
     * 删除好友
     *
     * @param friendUserId
     * @return
     */
    @DeleteMapping("/{friendUserId}")
    @ApiOperation("删除好友")
    public Result<Void> deleteFriend(@PathVariable Long friendUserId) {
        log.info("删除好友: friendUserId={}", friendUserId);
        friendService.deleteFriend(friendUserId);
        // 删除好友后，推送消息
        wsClient.sendMessage(friendUserId, "DELETE_FRIEND");
        return Result.success();
    }

    /**
     * 修改好友备注
     *
     * @param friendRemarkDto
     * @return
     */
    @PutMapping("/remark")
    @ApiOperation("修改好友备注")
    public Result<Void> changeFriendRemark(@RequestBody FriendRemarkDto friendRemarkDto) {
        log.info("修改好友备注: {}", friendRemarkDto);
        friendService.changeFriendRemark(friendRemarkDto);
        return Result.success();
    }


    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    @ApiOperation("获取用户信息")
    public Result<FriendGetVo> getUserInfo(@PathVariable Long userId) {
        log.info("获取用户信息: userId={}", userId);
        return Result.success(friendService.getUserInfo(userId));
    }
}
